from rest_framework import serializers
from os import path
from .models import Experiment,\
    ExperimentalProgram,\
    ExperimentProcedure,\
    Riddle


class ExperimentSerializers(serializers.HyperlinkedModelSerializer):
    """
    实验信息序列化器
    """
    readable_subject_name = serializers.SerializerMethodField()
    experiment_reviewer = serializers.SerializerMethodField()
    # readable_experiment_reviewer = serializers.SerializerMethodField()

    class Meta:
        model = Experiment
        fields = (
            'url', 'experiment_name', 'subject_name',
            'experiment_num', 'experimenter',
            'readable_subject_name',
            'experiment_reviewer',
            # 'readable_experiment_reviewer',
            'approval_status'
        )

    def get_readable_subject_name(self, obj):
        return obj.subject_name.subject_name

    def get_experiment_reviewer(self, obj):
        return obj.subject_name.subject_principal.username

    # def get_readable_experiment_reviewer(self, obj):
    #         return obj.experiment_reviewer.username


class ExperimentalProgramSerializers(serializers.HyperlinkedModelSerializer):
    """
    实验方案序列化器
    """
    readable_experiment_name = serializers.SerializerMethodField()
    annex_filename = serializers.SerializerMethodField()

    class Meta:
        model = ExperimentalProgram
        fields = (
            'url', 'experiment_name', 'experiment_program_name', 'purpose',
            'experimental_principle', 'annex', 'raw_material_name', 'purity',
            'raw_material_source', 'equipment_name', 'equipment_manufacturer',
            'device_model', 'quantity', 'equipment_steps', 'readable_experiment_name',
            'annex_filename'
        )

    def get_readable_experiment_name(self, obj):
        return obj.experiment_name.experiment_name

    def get_annex_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.annex.name
        return '' if filename is None else path.basename(filename)


class ExperimentProcedureSerializers(serializers.HyperlinkedModelSerializer):
    """
    实验过程序列化器
    """
    readable_experiment_name = serializers.SerializerMethodField()
    readable_experiment_program_name = serializers.SerializerMethodField()
    experiment_procedure_reviewer = serializers.SerializerMethodField()
    # readable_experiment_procedure_reviewer = serializers.SerializerMethodField()

    class Meta:
        model = ExperimentProcedure
        fields = (
            'url', 'experiment_name', 'experiment_program_name', 'experiment_code',
            'experimental_principle', 'experiment_record', 'experiment_phenomena', 'raw_material_name',
            'purity', 'raw_material_source', 'equipment_name',
            'equipment_manufacturer', 'device_model', 'quantity', 'equipment_steps', 'sample_num',
            'experimental_expectation',
            'experiment_procedure_reviewer',
            # 'readable_experiment_procedure_reviewer',
            'readable_experiment_name', 'readable_experiment_program_name', 'approval_status'
        )

       # depth = 1
    def get_readable_experiment_name(self, obj):
        return obj.experiment_name.experiment_name

    def get_readable_experiment_program_name(self, obj):
        return obj.experiment_program_name.experiment_program_name

    def get_experiment_procedure_reviewer(self, obj):
        return obj.experiment_name.subject_name.subject_principal.username

    # def get_readable_experiment_procedure_reviewer(self, obj):
    #     return obj.experiment_procedure_reviewer.username


class RiddleSerializer(serializers.HyperlinkedModelSerializer):
    """
    分析检测序列化器
    """
    readable_experiment_name = serializers.SerializerMethodField()
    riddle_report_filename = serializers.SerializerMethodField()
    annex_filename = serializers.SerializerMethodField()

    class Meta:
        model = Riddle
        fields = (
            'url', 'experiment_name', 'sample_num', 'sample_name', 'sample_status',
            'sample_quantity', 'delivery_personnel', 'testing_facility', 'attorney_number',
            'test_num', 'delivery_date', 'finish_date', 'riddle_report',
            'main_testing_equipment', 'experiment_amount', 'annex',
            'readable_experiment_name',
            'riddle_report_filename', 'annex_filename'
        )

    def get_readable_experiment_name(self, obj):
        return obj.experiment_name.experiment_name

    def get_riddle_report_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.riddle_report.name
        return '' if filename is None else path.basename(filename)

    def get_annex_filename(self, obj):
        # 若没有附件，则返回''，否则返回文件名
        filename = obj.annex.name
        return '' if filename is None else path.basename(filename)
